package com.raven.play.hello;

import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive;
import com.raven.play.hello.msg.Greet;
import com.raven.play.hello.msg.Greeted;

public class HelloWorld extends AbstractBehavior<Greet> {

    private HelloWorld(ActorContext<Greet> context) {
        super(context);
    }

    /**
     * 构造方法私有化, 然后提供一个静态的 create 方法, 并使用 Behaviors.setup 来创建
     * @return
     */
    public static Behavior<Greet> create() {
        return Behaviors.setup(HelloWorld::new);
    }

    @Override
    public Receive<Greet> createReceive() {
        return newReceiveBuilder().onMessage(Greet.class, this::onGreet).build();
    }

    private Behavior<Greet> onGreet(Greet command) {
        getContext().getLog().info("Hello {}!", command.whom());
        command.replyTo().tell(new Greeted(command.whom(), getContext().getSelf()));
        return this;
    }
}